using(['utils/fetchEnhancer',
    'pc/js/utils/XForm'], function(fetch, XForm){

var doc = document;
var btn = doc.getElementById('btn');
var verifycodebtn = doc.getElementById('verifycodebtn');
var agreement = doc.getElementById('agreement');
var agreementbtn = doc.getElementById('agreementbtn');
var f = new XForm();

f.checkOther('nick', {
    minLength: 2,
    infoText : '昵称允许使用字母、数字、汉字哟',
    errorText : '昵称丢了',
    callback: function() {
        fetch('/api/account/nickexist', {
            method: 'POST',
            body: 'nick=' + encodeURIComponent(doc.getElementById('nick').value)

        }).then(function(json){
            if(0 !== json.status) {
                f.setTipMessage('nick', '该昵称已被注册');
            }
        });
    }
});
f.checkOther('password', {
    minLength: 6,
    infoText : '输入至少 6 位密码',
    errorText : '密码太短啦'
});
f.checkEmail('email', {
    minLength: 6,
    infoText : '邮箱用于登录和找回密码',
    errorText : '邮箱填错啦'
});
f.checkOther('verifycode', {
    minLength: 4,
    infoText : '输入验证码',
    errorText : '验证码错啦'
});

var tickDown = function(x) {
    if(x < 1) {
        verifycodebtn.innerHTML = '重新获取验证码';
        verifycodebtn.disabled = false;

        return;
    }

    verifycodebtn.disabled = true;
    verifycodebtn.innerHTML = '获取中 ('+ x +')';

    setTimeout(function(){
        tickDown(x - 1);
    }, 1000);
};

// verifycode
verifycodebtn.onclick = function(e) {
    var email = document.getElementById('email').value;

    if('' === email) {
        return;
    }

    if(!f.rulesConfig.email.regular.test(email)) {
        return;
    }

    /* button 默认 disabled 就不可点击了
    if(verifycodebtn.disabled) {
        console.log('stopped');
        return;
    } */

    tickDown(30);
    fetch('/api/mail/sendregister', {
        method: 'POST',
        body: 'email=' + encodeURIComponent(email)

    }).then(function(data) {
        if(100006 === data.status) {
            f.setTipMessage('email', '该邮箱已经注册，可以直接登录');
        }
    });
};

agreementbtn.onclick = function(e) {
    // 原来是选中状态
    if(agreement.className.indexOf('active') > 0) {
        agreement.className = 'passport-item-checkbox';
        btn.disabled = true;

    } else {
        agreement.className = 'passport-item-checkbox active';
        btn.disabled = false;
    }
};

// do reg
btn.onclick = function() {
    var valid = f.validate();

    if(!valid) {
        return;
    }

    btn.disabled = true;
    USER_SDK.getInstance().apiRegister({
        nick: doc.getElementById('nick').value,
        password: doc.getElementById('password').value,
        email: doc.getElementById('email').value,
        verifyCode: doc.getElementById('verifycode').value
    }, function(data) {
        btn.disabled = false;

        if(100001 === data.status) {
            f.setTipMessage('nick', '用户名不合法');

            return;
        }

        if(100002 === data.status) {
            f.setTipMessage('password', '密码不合法');

            return;
        }

        if(100003 === data.status) {
            f.setTipMessage('email', '邮箱不合法');

            return;
        }

        if(100004 === data.status) {
            f.setTipMessage('verifycode', '验证码错误');

            return;
        }

        if(100005 === data.status) {
            f.setTipMessage('nick', '该昵称已被注册');

            return;
        }
        if(100006 === data.status) {
            f.setTipMessage('email', '该邮箱已被注册');

            return;
        }

        XUI.toast.success('注册成功', function() {
            window.location.href = '/login';
        });
    });
};

});
